import cv2
import numpy as np

# 读取图像
image = cv2.imread('../temp/image.jpg')
if image is None:
    print('Cannot read image file')
    exit(-1)

# 创建一个3x3的矩形结构元素
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))

# 进行开运算
opened = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel)

# 显示原始图像和开运算后的图像
cv2.namedWindow('Original Image', cv2.WINDOW_NORMAL)
cv2.namedWindow('Opened Image', cv2.WINDOW_NORMAL)
cv2.imshow('Original Image', image)
cv2.imshow('Opened Image', opened)

cv2.waitKey(0)
cv2.destroyAllWindows()
